package com.supreme.omsserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.benmanes.caffeine.cache.Cache;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.omsserver.entity.mysql.SaleOrder;
import com.supreme.omsserver.service.orm.SaleOrderOrmService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "order")
public class OrderController {

    @Resource
    private SaleOrderOrmService saleOrderOrmService;

    @Resource
    private Cache<String, SaleOrder> cacheSaleOrder;

    @GetMapping(value = "id/{orderId}")
    public AjaxResp getOrderOne(@PathVariable(value = "orderId") Long orderId) {
        SaleOrder saleOrder = saleOrderOrmService.getById(orderId);
        return AjaxResp.instance(saleOrder);
    }

    @GetMapping(value = "id-cache/{orderId}")
    public AjaxResp getCacheOrderOne(@PathVariable(value = "orderId") Long orderId) {
        SaleOrder saleOrder = cacheSaleOrder.get(SaleOrder.itemKey(orderId), s -> saleOrderOrmService.getById(orderId));
        return AjaxResp.instance(saleOrder);
    }

    @GetMapping(value = "code/{orderCode}")
    public AjaxResp getOrderOne(@PathVariable(value = "orderCode") String orderCode) {
        SaleOrder saleOrder = saleOrderOrmService.getByOrderCode(orderCode);
        return AjaxResp.instance(saleOrder);
    }

    @GetMapping(value = "code-cache/{orderCode}")
    public AjaxResp getCacheOrderOne(@PathVariable(value = "orderCode") String orderCode) {
        SaleOrder saleOrder = cacheSaleOrder.get(SaleOrder.itemKey(orderCode), s -> saleOrderOrmService.getByOrderCode(orderCode));
        return AjaxResp.instance(saleOrder);
    }

    @GetMapping(value = "one")
    public AjaxResp getOrderOne(SaleOrder saleOrderParams) {
        LambdaQueryWrapper<SaleOrder> lambdaQuery = Wrappers.lambdaQuery(SaleOrder.class);
        SaleOrder saleOrder = saleOrderOrmService.getOne(lambdaQuery);
        return AjaxResp.instance(saleOrder);
    }
}
